﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;


/// <summary>
/// db_connect 的摘要说明
/// </summary>
public class db_connect
{
    public static string g_strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\pos.mdb";
    public string conString
    {
        get { return g_strCon; }
        set { g_strCon = value; }
    }


    public static OleDbConnection m_dbconnect;
    public static int refCount=0;
    public static  bool m_openFlag=false;
    public string m_errorMsg;
    public db_connect()
    {
        // TODO: 在此处添加构造函数逻辑
        m_openFlag = false;
        //
    }
    public static void addRef()
    {
       
        if (m_openFlag)
        {
            //  throw (new Exception("db_connect:数据库已经打开!"));
            refCount++;
            return;
        }
        try
        {
            m_dbconnect = new OleDbConnection(g_strCon);
            m_dbconnect.Open();
         //   m_errorMsg = "";
            m_openFlag = true;
            refCount++;


        }
        catch (Exception ex)
        {
            throw (new Exception("db_connect::Open:" + ex.Message));

        }
    }
    public static void releaseRef()
    {
        
        refCount--;
        if (refCount <= 0)
        {
            try
            {
                m_openFlag = false;
                m_dbconnect.Close();
                m_dbconnect = null;
                return;
            }
            catch (Exception ex)
            {
                throw (new Exception("db_connect::Close:" + ex.Message));

            }
        }
      
    }
    public void Open()
    {
        addRef();
       
    }
    public void Close()
    {
        
        releaseRef();
            

    }
    public DataSet GetDataSet(string strSql)
    {
        if (!m_openFlag)
        {
            throw (new Exception("db_connect::m_openFlag状态期待true"));
        }
        try
        {
            OleDbDataAdapter command = new OleDbDataAdapter(strSql, m_dbconnect);
            DataSet data = new DataSet();
            command.Fill(data);
            return data;
        }
        catch (Exception ex)
        {
            throw (new Exception("db_connect::GetDataSet:" + strSql + ex.Message));

        }


    }
    public DataRow GetDataRow(string strSql)
    {
        if (!m_openFlag)
        {
            throw(new Exception("db_connect::m_openFlag状态期待true"));
         
        }
        try
        {
            OleDbDataAdapter command = new OleDbDataAdapter(strSql, m_dbconnect);
            DataSet data = new DataSet();
            command.Fill(data);
            DataTable dt = data.Tables[0];
            if (dt.Rows.Count == 0)
                return null;
          //  if (dt.Rows.Count > 1)
            //    throw (new Exception("想返回唯一行，但是实际上存在多行"));
            return dt.Rows[0];
          
           
        }
        catch (Exception ex)
        {
            throw(new Exception("db_connect::GetDataRow:" + strSql + ex.Message));
          
        }
       

    }
    public void ExeSql(string strSql)
    {
        if (!m_openFlag)
        {
            throw (new Exception("db_connect::m_openFlag状态期待true"));
        }
        try
        {
            OleDbCommand command = new OleDbCommand(strSql, m_dbconnect);
            command.ExecuteNonQuery();
            return;
        }
        catch (Exception ex)
        {
            throw (new Exception("db_connect::ExeSql:" + strSql + ex.Message));
        }


    }
}
